package cn.dckeji.gameBox.controller;

import cn.dckeji.gameBox.common.utils.DataUtil;
import cn.dckeji.gameBox.common.utils.MD5;
import cn.dckeji.gameBox.common.utils.ResultUtil;
import cn.dckeji.gameBox.dto.GameType;
import cn.dckeji.gameBox.dto.Result;
import cn.dckeji.gameBox.dto.UserStatis;
import cn.dckeji.gameBox.entity.*;
import cn.dckeji.gameBox.service.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/page")
public class PageController {
    @Autowired
    private UserService userService;

    @Autowired
    private InformationService informationService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private InfoService infoService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private BannerService bannerService;
    /**
     * 去登录页
     *
     * @return
     */
    @GetMapping("toLogin")
    public String toLogin() {
        return "login/login";
    }


    /**
     * 去后台首页
     */
    @GetMapping("toAdminIndex")
    public String toAdminIndex() {
        return "index";
    }


    /**
     * 显示主页
     *
     * @return
     */
    @GetMapping("showMain")
    public String showMain() {
        return "main";
    }
    /**
     * 管理员登录
     * @param userName
     * @param password
     */
    @PostMapping("login")
    @ResponseBody
    public Result login(@RequestParam("username") String userName, @RequestParam("password")String password, HttpSession session) {
        EntityWrapper<User> entityWrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(userName)) {
            entityWrapper.eq("loginName",userName);
        }
        User user=userService.selectOne(entityWrapper);
        if (user == null) {
            return ResultUtil.error("账号或密码不正确");
        }
        if(user.getState()==0){
            return ResultUtil.error("账户已被禁用");
        }
        String managersPwd = user.getLoginPwd();
        if (!MD5.CreateMD5(MD5.CreateMD5(password)).equals(managersPwd)) {
            return ResultUtil.error("账号或密码不正确");
        }
        session.setAttribute("admin",user.getRealName());
        session.setAttribute("adminUser",user);
        return ResultUtil.success();
    }

    @GetMapping("toEditState")
    public String toEditState(Integer id,HttpServletRequest request){
        User user=userService.selectById(id);
        request.setAttribute("user",user);
        return "user/stateEdit";
    }

    @GetMapping("tuichu")
    public String tuichu(HttpSession session) {
        session.removeAttribute("admin");//清空session信息
        session.removeAttribute("adminUser");//清空session信息
        session.invalidate();//清除 session 中的所有信息
        return "login/login";
    }
    @GetMapping("toChangePassWord")
    public String toChangePassWord(){
        return "changePassWord";
    }
    /**
     *  去客户管理列表页
     * @return
     */
    @GetMapping("toAddUserList")
    public String toAddUserList(){
        return "loginLog/addUserList";
    }
    /**
     *  去客户管理列表页
     * @return
     */
    @GetMapping("toCustomerList")
    public String toCustomerList(){
        return "customer/customerList";
    }

    /**
     *  去保险信息管理列表页
     * @return
     */
    @GetMapping("toInsuranceList")
    public String toInsuranceList(){
        return "insurance/insuranceList";
    }

    /**
     *  去用户管理列表页
     * @return
     */
    @GetMapping("toUserList")
    public String toUserList(){
        return "user/userList";
    }

    /**
     * 去用户添加和修改页面
     * @return
     */
    @GetMapping("toEditUser")
    public String toEditUser(Integer userId, HttpServletRequest request){
        User user=userService.selectById(userId);
        request.setAttribute("user",user);
        return "user/userEdit";
    }
    /**
     * 去信息维护页面
     * @return
     */
    @GetMapping("toInformationList")
    public String toInformationList(Integer type,HttpServletRequest request){
        EntityWrapper<Information> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("type",type);
        Information information=informationService.selectOne(entityWrapper);
        request.setAttribute("info",information);
        if (1 == type) {
            return "information/companyEdit";
        }else if(2==type){
            return "information/newsEdit";
        }else if(3==type) {
            return "information/goodCardEdit";
        }else{
            return "information/emailEdit";
        }
    }
    /**
     *  去banner管理列表页
     * @return
     */
    @GetMapping("toBannerList")
    public String toBannerList(){
        return "banner/bannerList";
    }
    /**
     * 去用户添加和修改页面
     * @return
     */
    @GetMapping("toEditBanner")
    public String toEditBanner(Integer id,Integer gameType,HttpServletRequest request){
        List<GameType> typeList = getGameTypes();
        request.setAttribute("typeList",typeList);
        request.setAttribute("gameType",gameType);
        Banner banner=bannerService.selectById(id);
        request.setAttribute("banner",banner);
        return "banner/bannerEdit";
    }

    private List<GameType> getGameTypes() {
        GameType type1 = new GameType();
        type1.setId(1);
        type1.setName("二维码");
        GameType type2 = new GameType();
        type2.setId(2);
        type2.setName("小游戏");
        GameType type3 = new GameType();
        type3.setId(3);
        type3.setName("h5游戏");
        List<GameType> typeList = new ArrayList<>();
        typeList.add(type1);
        typeList.add(type2);
        typeList.add(type3);
        return typeList;
    }

    /**
     * 去启动盒子日志页面
     * @return
     */
    @GetMapping("toStartBoxList")
    public String toStartBoxList(){
        return "loginLog/startBoxList";
    }

    /**
     * 去启动盒子日志页面
     * @return
     */
    @GetMapping("toContinueList")
    public String toContinueList(){
        return "loginLog/continueList";
    }


    /**
     * 去开始游戏日志页面
     * @return
     */
    @GetMapping("toStartGameList")
    public String toStartGameList(){
        return "loginLog/startGameList";
    }

    /**
     * 去游戏列表页面
     * @return
     */
    @GetMapping("toInfoList")
    public String toInfoList(HttpServletRequest request){
        List<Type> type=typeService.selectList(null);
        request.setAttribute("typeList",type);
        return "info/infoList";
    }

    /**
     * 去用户添加页面
     * @return
     */
    @GetMapping("toAddGameInfo")
    public String toAddBanner(HttpServletRequest request){
        initInfoTypes(request);
        return "info/infoAdd";
    }

    private void initInfoTypes(HttpServletRequest request) {
        List<Type> type = typeService.selectList(null);
        request.setAttribute("typeList", type);
        List<GameType> typeList = getGameTypes();
        request.setAttribute("cateList", typeList);
    }

    /**
     * 去游戏修改页面
     * @return
     */
    @GetMapping("toEditGameInfo")
    public String toEditGameInfo(Integer id,HttpServletRequest request){
        initInfoTypes(request);
        Info info=infoService.selectById(id);
        request.setAttribute("info",info);
        return "info/infoEdit";
    }
    /**
     * 去用户添加和修改页面
     * @return
     */
    @GetMapping("toEditGameState")
    public String toEditGameState(Integer id,HttpServletRequest request){
        Info info=infoService.selectById(id);
        request.setAttribute("info",info);
        return "info/stateEdit";
    }

    /**
     * 去日增统计页面
     * @return
     */
    @GetMapping("toAddUserStatis")
    public String toAddUserStatis(String date,HttpServletRequest request){
        String starttime=null;
        String enddate=null;
        if(StringUtils.isNotBlank(date)){
            starttime = date.substring(0, 11);
            enddate = date.substring(date.length() - 10);
        }else{
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            starttime= DataUtil.getPastDate(7);
            enddate=sdf.format(new Date());
        }
        List<String> dataList=DataUtil.getPerDaysByStartAndEndDate(starttime,enddate,"yyyy-MM-dd");
        Map<String,Object> maps=new HashMap<>();
        maps.put("startTime",starttime);
        maps.put("endTime",enddate);
        List<UserStatis>  customerList=customerService.getStatis(maps);
        String xDate="[";
        String yDate="[";
        for(int i=0;i<dataList.size();i++){
            int count=0;
            for(UserStatis userStatis:customerList){
                if(userStatis.getCreateTime().startsWith(dataList.get(i))){
                    count=userStatis.getCount();
                    break;
                }
            }
            if(i==dataList.size()-1){
                xDate+="'"+dataList.get(i)+"'";
                yDate+=count;
            }else{
                xDate+="'"+dataList.get(i)+"',";
                yDate+=count+",";
            }
        }
         xDate+="]";
         yDate+="]";
         request.setAttribute("startTime",starttime);
        request.setAttribute("endTime",enddate);
         request.setAttribute("xDate",xDate);
        request.setAttribute("yDate",yDate);
        return "loginLog/addUserStatis";
    }


}
